package com.zzty.springmybatis.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzty.springmybatis.DAO.QuestionDAO;
import com.zzty.springmybatis.DAO.SelfExamDAO;
import com.zzty.springmybatis.DAO.SelfExamTransationDAO;
import com.zzty.springmybatis.DAO.UserDAO;
import com.zzty.springmybatis.entity.SelfExam;
import com.zzty.springmybatis.entity.User;
import com.zzty.springmybatis.service.SelfExamService;

@Service
public class SelfExamServiceImpl implements SelfExamService {
	@Autowired
	SelfExamDAO sed;

	@Autowired
	SelfExamTransationDAO setd;

	@Autowired
	UserDAO ud;

	@Autowired
	QuestionDAO qd;

	public boolean judgeQstExist(int id) {

		Date date = new Date();
		String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
		User u = new User();
		u.setId(id);
		System.out.println("user:" + ud.selectUserInfo(u).toString());
		Integer clazzid = ud.selectUserInfo(u).getClazzId();
		try {
			if (sed.qstExist(clazzid, dateStr) == null) {

				return false;
			}
			if (sed.qstExist(clazzid, dateStr).getQuestions() != null) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}

	public ArrayList searchQstId(int cName) {
		ArrayList<Integer> List = new ArrayList<Integer>();
		String[] strarr;
		Date date = new Date();
		String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
		try {
			String qid = sed.searchQstId(dateStr, cName);
			strarr = qid.split("\\|");
			for (int i = 0; i < strarr.length; i++) {
				List.add((Integer.parseInt(strarr[i])));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return List;
	}

	public ArrayList searchQstContent(ArrayList arr) {
		int[] iarr = new int[arr.size()];
		for (int i = 0; i < arr.size(); i++) {
			iarr[i] = (Integer) arr.get(i);
		}
		ArrayList<String> questions = new ArrayList<String>();
		try {
			for (int i = 0; i < arr.size(); i++) {

				questions.add(qd.selectQuecontent(iarr[i] + "").getContent() + "\n");
				// questions.add(sed.searchQstContent(iarr[i]) + "\n");
				// questions.add("test" + "\n");
				//
				// sarr[i]
				// =
				// sed.searchQstContent(iarr[i]);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return questions;
	}

	public String searchClazzByUserId(int id) {
		try {
			User u = new User();
			u.setId(id);

			return (ud.selectUserInfo(u).getClazzId() + "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Integer searchExamId(int userId) {
		Date date = new Date();
		String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
		try {
			return (sed.searchExId(userId, dateStr));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public Integer searchIdByDateAndClass(String date, int clazzId) {
		if (sed.searchIdByDateAndClass(date, clazzId) == null) {
			return null;

		}
		return (sed.searchIdByDateAndClass(date, clazzId));
	}

	public ArrayList searchQstId(String date, int cName) {
		ArrayList<Integer> List = new ArrayList<Integer>();
		String[] strarr;
		String qid;
		try {
			qid = sed.searchQstId(date, cName);
			strarr = qid.split("\\|");
			for (int i = 0; i < strarr.length; i++) {
				List.add((Integer.parseInt(strarr[i])));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return List;
	}

	public Integer saveSelfExam(SelfExam se) {
		try {
			return sed.insertSelfExam(se);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public void updateWinner(int examId, int winnerId) {
		sed.updateWinner(examId, winnerId);

	}

	@Override
	public List<SelfExam> searchSelfExamByClazzId(Integer clazzId, Integer page) {
		return sed.searchSelfExamByClazzId(clazzId, page);
	}

	@Override
	public List<SelfExam> searchSelfExamByClazzId(Integer clazzId) {
		return sed.searchSelfExamOnlyByClazzId(clazzId);
	}

	@Override
	public Integer searchNewId(String date, String questions, Integer clazzId) {
		return sed.searchNewId(date, questions, clazzId);
	}

	@Override
	public List<SelfExam> findLastFiveTeamWin(Integer clazzId) {
		
		return sed.findLastFiveTeamWin(clazzId);
	}

	@Override
	public List<SelfExam> searchSelfExamByWinnerIdAndClazzId(Integer clazzId) {
		return sed.searchSelfExamByWinnerIdAndClazzId(clazzId);

	}

	@Override
	public List<SelfExam> searchTenSelfExamByClazzId(Integer clazzId, Integer page) {

		return sed.searchTenSelfExamByClazzId(clazzId, page);
	}

}
